<?php
/**
 * 体检报告
 *
 * User: kaikaic
 * Date: 2018/12/2
 * Time: 14:10
 */

namespace app\exam\controller;

use app\exam\model\ReportModel;
use app\exam\service\AccountService;
use app\exam\service\MecService;
use app\exam\service\MecSetService;
use app\exam\service\MessageService;
use app\exam\service\OrderService;
use app\exam\service\ReportService;
use controller\BasicAdmin;
use service\ConfigService;
use service\ToolsService;
use think\Db;

class Report extends BasicAdmin {

    protected $table = "ExamReport";

    public function index() {
        $this->title = "体检报告";

        $mobile = $this->request->get("mobile");
        $orderSN = $this->request->get("order_sn");

        $db = Db::name($this->table);
        $db->where(['status'=>1]);

        if ($mobile) {
            $db->where(['mobile'=>$mobile]);
        }
        if ($orderSN) {
            $db->where(['order_sn'=>$orderSN]);
        }

        return parent::listRecord($db, "", true, "id desc");
    }

    /**
     * 体检报告-添加
     */
    public function add() {
        if ($this->request->isGet()) {
            $vo = array();
            return view('form', [
                'title'=>'添加体检报告',
                'vo'=>$vo,
            ]);
        }

        if ($this->request->isPost()) {
            $data = $this->request->post();

            if ($this->doInsertAction($data)) {
                $this->success('添加成功！', '');
            }else {
                $this->error('添加失败，请稍候再试！');
            }
        }
    }

    /**
     * 体检报告-编辑
     */
    public function edit() {
        if ($this->request->isGet()) {
            $id = $this->request->get('id', '');
            if (empty($id)) {
                $this->error('参数错误，请稍候再试！');
            }

            $record = ReportService::getInstance()->find($id);
            if (empty($record)) {
                $this->error('记录不存在，请重新操作!');
            }

            return view('form', [
                'title'=>'编辑体检报告',
                'vo'=>$record,
            ]);
        }

        if ($this->request->isPost()) {
            $id = $this->request->post('id', '');
            if (empty($id)) {
                $this->error('参数错误，请稍候再试！');
            }

            $data = $this->request->post();
            if ($this->doInsertAction($data)) {
                $this->success('编辑成功！', '');
            }else {
                $this->error('编辑失败，请稍候再试！');
            }
        }
    }

    /**
     * 删除-体检中心
     */
    public function cancel() {
        $id = intval($this->request->post('id', ''));
        if (empty($id)) {
            $this->error('参数错误!');
        }

        $record = ReportService::getInstance()->find($id);
        if (empty($record)) {
            $this->error('记录不存在，请重新操作!');
        }

        $result = ReportService::getInstance()->update(array('status'=>2), $id);
        if ($result) {
            $this->success('删除成功!', '');
        }else {
            $this->error('删除失败，请重新再试!');
        }
    }

    /**
     * 体检报告-批量导入
     */
    public function import() {
        if ($this->request->isGet()) {
            return view('import', [
                'title'=>'导入体检报告',
            ]);
        }

        if ($this->request->isPost()) {
            if ($this->doImportAction()) {
                $this->success('导入完成！', "");
            }else {
                $this->error('导入失败，请稍候再试！');
            }
        }
    }

    private function doImportAction() {
        $data = $this->request->post();
        $excelFile = $data['report_excel'];
        if (!$excelFile) {
            return false;
        }

        $reportModel = new ReportModel();
        $reportModel->readExcelFile($excelFile);

        return true;
    }

    /**
     * 数据写入
     */
    protected function doInsertAction($data) {
        if (!$data['name']) {
            return false;
        }

        // 用户信息
        $mobile = $data['mobile'];
        if (!ToolsService::checkMobile($mobile)) {
            return false;
        }

        $account = AccountService::getInstance()->single(array('mobile'=>$mobile));
        if (!$account) {
            return false;
        }

        // 记录是否已存在
        $record = ReportService::getInstance()->single(array('mobile'=>$mobile, 'order_sn'=>$data['order_sn']));

        $data['account_id'] = $account['id'];
        $data['real_name'] = $account['real_name'];
        $data['identity_card'] = $account['identity_card'];

        if (empty($data['id'])) {
            if ($record) {
                return false;
            }

            $data['create_time'] = time();

            $result = ReportService::getInstance()->insert($data);
        } else {
            $id = intval($data['id']);
            if ($record && ($record['id'] != $id)) {
                return false;
            }

            $result = ReportService::getInstance()->update($data, $id);
        }

        return $result;
    }
}

